package com.angel.bo.business.service.impl;

import com.angel.bo.beans.webstart.WebStart;
import com.angel.bo.business.service.WebStartService;
import com.angel.bo.common.msg.WebReturn;
import com.xiaoleilu.hutool.io.FileUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URL;

@Service
@Transactional(rollbackFor = Exception.class)
public class WebStartServiceImpl implements WebStartService{

    /**
     * 动态生成JNLP文件
     * @param webStart
     * @return
     */
    @Override
    public WebReturn dynamicGenerationJNLP(WebStart webStart) {

        StringBuffer JNLP = new StringBuffer();

        JNLP.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<!--codebase 属性指出搜索应用程序资源的顶级URL, 下面的icon/jar元素都是以这个URL为基本. -->\n" +
                "<jnlp codebase=\"http://localhost:8080/JavaWebStart/\">\n" +
                "    <information>\n" +
                "        <!-- 在\"开始\"-\"运行\"菜单中输入\"javaws\"或\"javaws -viewer\"启动Web Start, \n" +
                "        会看到客户端已经安装的webstart应用程序 -->\n" +
                "        <!-- title:应用程序标题 vendor:供应商;title/vendor元素是必须， \n" +
                "        会显示在用\"javaws -viewer\"命令打开的应用程序缓存查看器（Java \n" +
                "            Application Cache Viewer）中 -->\n" +
                "            <title>Hello World</title>\n" +
                "            <vendor>Lively Corporation</vendor>\n" +
                "            <description>HelloWorld Test Example</description>\n" +
                "        <!--homepage ：存放有关应用程序的相关文档的URL，如help文件等，\n" +
                "        仅仅是description作用-->\n" +
                "            <homepage href=\"http://127.0.0.1:8080/JavaWebStart/index.html\" \n" +
                "            mce_href=\"http://127.0.0.1:8080/JavaWebStart/index.html\" />  \n" +
                "         <!-- \n" +
                "            icon 指定图标会显示在应用程序缓存查看器中（作为java web应用程序的图标），\n" +
                "                        在查看器中新建JavaWebStart快捷方式到桌面时也会显示为快捷方式图标，只支持GIF/JPEG格式，其它格式无效 \n" +
                "        -->\n" +
                "        <icon kind=\"splash\" href=\"images/Fish13.jpg\" mce_href=\"images/Fish13.jpg\" /> \n" +
                "        <!-- 允许离线启动，可以使用javaws -offline命令-->  \n" +
                "         <offline-allowed/>      \n" +
                "    </information>\n" +
                "    <resources>\n" +
                "        <!--  \n" +
                "            指定客户端需要安装的j2se版本，下面指定为1.5，也可以用1.5+表示1.5版本以上的， 如果这里设置版本为1.5，而我机装的是1.6（所以我这里设置为1.7也会出现后面的问题），那么提示正在请求 JRE 1.5，过一段时间提示：无法启用该应用程序，提示手动更新j2se版本  \n" +
                "            改为1.5+就可以用1.6的JRE来运行它了  \n" +
                "        -->  \n" +
                "        <j2se version=\"1.5+\" />  \n" +
                "        <!-- \n" +
                "            指定要下载到本地的jar文件(注意，所有的文件都需要打包才能够下载)， 可以包含一些资源文件，如icons/configuration files，可以使用getResource方法取得 \n" +
                "        -->  \n" +
                "        <jar href=\"HelloWorld.jar\" mce_href=\"HelloWorld.jar\" />  \n" +
                "    </resources> \n" +
                "    <!--  此项是指开放用户本机的所有权限给应用程序，这一项会导致弹出一个数字签名对话框。-->\n" +
                "    <security>\n" +
                "        <all-permissions/>\n" +
                "    </security>\n" +
                "     <!--application-desc 必须，指定JavaWebStart启动时执行jar文件中的哪个类-->  \n" +
                "    <application-desc main-class=\"jws.HelloWord\" />   \n" +
                "</jnlp>");


        JNLP = new StringBuffer();
        JNLP.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<jnlp spec=\"1.0+\" \n" +
                "        codebase=\"\">\n" +
                "    <information>\n" +
                "        <title>Hello World</title>\n" +
                "        <vendor>angel</vendor>\n" +
                "    </information>\n" +
                "    <resources>\n" +
                "        <!-- Application Resources -->\n" +
                "        <j2se version=\"1.7+\"\n" +
                "              href=\"http://java.sun.com/products/autodl/j2se\"/>\n" +
                "        <jar href=\"downloadWebExecuteOJar/HelloWorld.jar\" main=\"true\" />\n" +
                "\n" +
                "    </resources>\n" +
                "    <application-desc\n" +
                "         name=\"Dynamic Tree Demo Application\"\n" +
                "         main-class=\"HelloWord\">\n" +
                "     </application-desc>\n" +
                "     <update check=\"background\"/>\n" +
                "</jnlp>\n>");

        JNLP = new StringBuffer();
        JNLP.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<jnlp spec=\"1.0+\" \n" +
                "        codebase=\"\">\n" +
                "    <information>\n" +
                "        <title>Case Cloud</title>\n" +
                "        <vendor>ANGEL</vendor>\n" +
                "    </information>\n" +
                "    <resources>\n" +
                "        <!-- Application Resources -->\n" +
                "        <j2se version=\"1.7+\"\n" +
                "              href=\"http://java.sun.com/products/autodl/j2se\"/>\n" +
                "        <jar href=\"downloadWebExecuteOJar/case-cloud-1.0-SNAPSHOT.jar\" main=\"true\" />\n" +
                "\n" +
                "    </resources>\n" +
                "    <security>\n" +
                "        <all-permissions/>\n" +
                "    </security>\n" +
                "    <application-desc\n" +
                "         name=\"Case Cloud Application\"\n" +
                "         main-class=\"CaseTest\">\n" +
                "       <argument>TEST#DEMO#000000</argument>"+
                "       <argument>" +  webStart.getOption() +"</argument>"+
                "     </application-desc>\n" +
                "     <update check=\"background\"/>\n" +
                "</jnlp>\n>");
        WebReturn webReturn = new WebReturn();
        webReturn.setData(JNLP.toString());

        return webReturn;
    }


    /**
     * 下载webstart执行的jar文件
     * @param jarName 需要下载执行的jar包名称
     * @param request
     * @param response
     */
    @Override
    public void downloadWebExecuteOJar(String jarName,HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.reset();
        response.setContentType("application/octet-stream; charset=UTF-8");

        ClassLoader classLoader = getClass().getClassLoader();
        /**
         getResource()方法会去classpath下找这个文件，获取到url resource, 得到这个资源后，调用url.getFile获取到 文件 的绝对路径
         */
        URL url = classLoader.getResource("webStartJar/"+jarName);
        File file = new File(url.getFile());
        response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes(), "ISO8859-1"));
        response.addHeader("Content-Length", "" + file.length());

        FileUtil.writeToStream(file,response.getOutputStream());
    }
}
